package edu.westga.cs3152.project2;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * The SpellChecker reads in a file word by word, seeing if it contains any misspelled words 
 * (by comparing them against a Dictionary) and offers suggestions for replacement words
 * (by using the SpellingSuggestor)
 * 
 * @author Lewis Williams
 * @version 2011.3.18
 */
public class SpellChecker {
	
	//The dictionary to compare words against 
	private Dictionary dictionary = new Dictionary("words.txt");
	//Keeps track of misspelled words that get found in the document.
    private ArrayList<String> misspelledWords = new ArrayList<String>();
	
	/**
	 * A default constructor for Dictionary that does nothing.
	 */
	public SpellChecker(){}

	/**
	 * Finds misspelled words in a document and adds them to an ArrayList.
	 */
	public void checkSpelling(String filePath) {
		try{
    		//Creates the file
    		File words = new File(filePath);
    		//Creates the scanner to read the file.
    		Scanner scan = new Scanner(words);
    		//The word currently being read in from the file
    		String currentWord = "";
    		//The word line being read in from the file
    		String currentLine = "";
    		 //Clears out the list of misspelled words in case this spell checker has been used in the past.
    			misspelledWords.clear();
    	
    		while(scan.hasNext()){
    			//Adds a blank space to the end of the current line so that the last word of the line won't be left out.
    			currentLine = scan.nextLine() + " ";
    			//A counter to help know when the end of the line is reached.
    			int lineLength = 0;
    			//The beginning index of the word being spell checked.
    			int beginWordIndex = 0;
    			//The ending index of the word being spell checked.
    			int endWordIndex = 0;
    			
    			while(lineLength < currentLine.length()){
    				endWordIndex = currentLine.indexOf(" ", beginWordIndex);
    				currentWord = currentLine.substring(beginWordIndex, endWordIndex);
    				lineLength += currentWord.length() + 1;
    				//Makes the current word lower case
    				currentWord = currentWord.toLowerCase();    				
    				//Strips out any punctuation attached to the current word
    				currentWord = currentWord.replaceAll("[^a-z]", "");
    				//Removes all white space to deal with an error in how I read in words.
    				currentWord = currentWord.trim();
    				
    				//If the dictionary does not contain the current word it is added to the misspelled word list
    				if(!dictionary.contains(currentWord) && !currentWord.equalsIgnoreCase("") && currentWord.length() > 1){
    					misspelledWords.add(currentWord);
    				}
    				beginWordIndex = endWordIndex + 1;
    			}
    		}
    	}
    	//Catches FileNotFoundException if the document to be checked cannot be found.
    	catch(FileNotFoundException fnfe){
    		System.out.println("The document cannot be found");
    	}
	}
	
	public ArrayList<String> getMisspelledWords(){
		ArrayList<String> misspelledWordsCopy = new ArrayList<String>();
		for(int i = 0; i < misspelledWords.size(); i++){
			misspelledWordsCopy.add(misspelledWords.get(i));
		}
		return misspelledWordsCopy;
	}

}
